<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//


require_once(TANGRA_MAIN_DIR.'core/config_loader_file.class.php');
require_once(TANGRA_MAIN_DIR.'db/db_config.class.php');
require_once(TANGRA_MAIN_DIR.'db/db_config_loader_file.class.php');
require_once($PATHS->get_hidden().'inc/_sys/dbal_conn.class.php');
require_once($PATHS->get_hidden().'inc/modules/session_in_db/session_in_db.class.php');
require_once($PATHS->get_hidden().'inc/_sys/session_dbc.class.php');
require_once($PATHS->get_hidden().'inc/_sys/site_dbc_pnt.class.php');


$conf_loader = new Config_Loader_File($PATHS->get_hidden().'conf/db_session.conf');


$dsn = $conf_loader->get_conf_value('DB_DSN');
$table = $conf_loader->get_conf_value('DB_SESSIONS_TABLE');
$force_gc = $conf_loader->get_conf_value('FORCE_GARBAGE_COLLECTION', false)
							? $conf_loader->get_conf_value('FORCE_GARBAGE_COLLECTION', false)
							: 1;
$probability = $conf_loader->get_conf_value('GC_PROBABILITY', false)
							? $conf_loader->get_conf_value('GC_PROBABILITY', false)
							: 1;
$divisor =  $conf_loader->get_conf_value('GC_DIVISOR', false)
							? $conf_loader->get_conf_value('GC_DIVISOR', false)
							: 100;

global $SESSION_IN_DB;
global $_SITE_DBC_PNT;

if (trim($dsn)) {
 	if(strtolower(trim($dsn)) == 'dbc_pnt' || strtolower(trim($dsn)) == 'pnt') {
		$db_config_loader = new DB_Config_Loader_File($PATHS->get_hidden().'conf/db.conf');
		$db_config = new DB_Config();
		$db_config_loader->load_db_config($db_config);
		$_SITE_DBC_PNT = new Site_DBC_PNT($db_config);
		$SESSION_IN_DB = new Session_In_DB($_SITE_DBC_PNT, $table, $force_gc, $probability, $divisor);
 	} else {
		$session_dbc = new Session_DBC($dsn);
		$SESSION_IN_DB = new Session_In_DB($session_dbc, $table, $force_gc, $probability, $divisor);
 	}
} else {
	$db_config_loader = new DB_Config_Loader_File($PATHS->get_hidden().'conf/db.conf');
	$db_config = new DB_Config();
	$db_config_loader->load_db_config($db_config);
	$_SITE_DBC_PNT = new Site_DBC_PNT($db_config);
	$SESSION_IN_DB = new Session_In_DB($_SITE_DBC_PNT, $table, $force_gc, $probability, $divisor);
}


session_set_save_handler(	array($SESSION_IN_DB, 'open'),
							array($SESSION_IN_DB, 'close'),
							array($SESSION_IN_DB, 'read'),
							array($SESSION_IN_DB, 'write'),
							array($SESSION_IN_DB, 'destroy'),
							array($SESSION_IN_DB, 'gc')
						);

register_shutdown_function('session_write_close');
